package com.bwie.auth.config;

import com.bwie.common.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Configuration
@Log4j2
public class GlobalExceptionHandler {

    /**
     * 全局的异常处理
     * @param exception
     * @return
     */
    @Resource
    private HttpServletRequest request;

    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result<String> illegalArgumentExceptionHandler(IllegalArgumentException exception){
        log.error("请求异常：[{}]", exception.getMessage(), exception);
        return Result.error(exception.getMessage());
    }

    @ExceptionHandler(value = RuntimeException.class)
    public Result<String> RuntimeException(RuntimeException exception){
        String requestURI = request.getRequestURI();
        log.error("请求异常：[{}]",requestURI+exception.getMessage());
        return Result.error(exception.getMessage());
    }

}
